package com.iss.umservice.common;

import java.awt.Component;
import java.awt.Graphics;
import java.awt.Rectangle;
import java.beans.PropertyChangeListener;
import java.beans.PropertyEditor;
import java.text.DateFormat;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.propertyeditors.CustomDateEditor;

/**
 * 用来处理java.sql.Date的转换问题
 * @author luzi
 *
 */
public class SqlDatePropertyEditor implements PropertyEditor {
	private final Logger logger = LoggerFactory.getLogger(getClass());
	// private DateFormat fmt;
	// private boolean allowEmpty;
	private CustomDateEditor dateEditor;

	public SqlDatePropertyEditor(DateFormat fmt, boolean allowEmpty) {
		super();
		// this.fmt = fmt;
		// this.allowEmpty = allowEmpty;
		dateEditor = new CustomDateEditor(fmt, allowEmpty);
	}

	@Override
	public void setValue(Object value) {
		logger.debug("start[setValue]" + value);
		if(value == null)
			dateEditor.setValue(value);
		else{
			java.util.Date tmp = new java.util.Date (((java.sql.Date)value).getTime());
			dateEditor.setValue(tmp);
		}
		logger.debug("end[setValue]");
	}

	@Override
	public Object getValue() {
		logger.debug("start[getValue]");
		java.util.Date tmp =(java.util.Date) dateEditor.getValue();
		if(tmp==null)
			return null;
		java.sql.Date re = new java.sql.Date(tmp.getTime());
		logger.debug("end[getValue]" + re);
		return re;
	}

	@Override
	public boolean isPaintable() {
		logger.debug("start[isPaintable]");
		boolean re = dateEditor.isPaintable();
		logger.debug("end[isPaintable]" + re);
		return re;
	}

	@Override
	public void paintValue(Graphics gfx, Rectangle box) {
		logger.debug("start[paintValue]");
		dateEditor.paintValue(gfx, box);
		logger.debug("end[paintValue]");
	}

	@Override
	public String getJavaInitializationString() {
		logger.debug("start[getJavaInitializationString]");
		String re = dateEditor.getJavaInitializationString();
		logger.debug("end[getJavaInitializationString]" + re);
		return re;
	}

	@Override
	public String getAsText() {
		logger.debug("start[getAsText]");
		String re = dateEditor.getAsText();
		logger.debug("end[getAsText]" + re);
		return re;
	}

	@Override
	public void setAsText(String text) throws IllegalArgumentException {
		logger.debug("start[setAsText]");
		dateEditor.setAsText(text);
		logger.debug("end[setAsText]");
	}

	@Override
	public String[] getTags() {
		logger.debug("start[getTags]");
		String[] re = dateEditor.getTags();
		logger.debug("end[getTags]" + re);
		return re;
	}

	@Override
	public Component getCustomEditor() {
		logger.debug("start[getCustomEditor]");
		Component re = dateEditor.getCustomEditor();
		logger.debug("end[getCustomEditor]" + re);
		return re;
	}

	@Override
	public boolean supportsCustomEditor() {
		logger.debug("start[supportsCustomEditor]");
		boolean re = dateEditor.supportsCustomEditor();
		logger.debug("end[supportsCustomEditor]" + re);
		return re;
	}

	@Override
	public void addPropertyChangeListener(PropertyChangeListener listener) {
		logger.debug("start[addPropertyChangeListener]");
		dateEditor.addPropertyChangeListener(listener);
		logger.debug("end[addPropertyChangeListener]");
	}

	@Override
	public void removePropertyChangeListener(PropertyChangeListener listener) {
		logger.debug("start[removePropertyChangeListener]");
		dateEditor.removePropertyChangeListener(listener);
		logger.debug("end[removePropertyChangeListener]");
	}
}